//
//  FuJinViewController.m
//  teaching
//
//  Created by yishangpin2 on 15/5/8.
//  Copyright (c) 2015年 衣尚品1. All rights reserved.
//

#import "FuJinViewController.h"

@interface FuJinViewController ()<UIGestureRecognizerDelegate>{
    
    BMKPointAnnotation* pointAnnotation;
    
    NSMutableArray *latitudeArray;
    NSMutableArray *longitudeArray;
    NSMutableArray *TitleArray;
    NSMutableArray *SubtitleArray;
}


@end

@implementation FuJinViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    //适配ios7
    if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0))
    {
        //        self.edgesForExtendedLayout=UIRectEdgeNone;
        self.navigationController.navigationBar.translucent = NO;
    }
    //    [self addCustomGestures];//添加自定义的手势
    
    _locService = [[BMKLocationService alloc]init];
    
    
    
    latitudeArray = [[NSMutableArray alloc]initWithObjects:@"31.516679",@"31.042484",@"31.503850",@"31.529247",@"31.355915",@"31.448462",@"31.226633",@"31.184330",@"31.436509",@"31.336666",@"31.220269",@"31.176581",@"31.456720",@"31.545076",@"31.080897", nil];
    
    longitudeArray = [[NSMutableArray alloc]initWithObjects:@"121.243683",@"120.990683",@"121.220719",@"120.824691",@"121.037519",@"121.239691",@"121.190384",@"121.003041",@"121.052165",@"121.002072",@"121.038907",@"121.228214",@"121.226742",@"120.961592",@"120.983681", nil];
    
    TitleArray = [[NSMutableArray alloc]initWithObjects:@"Rose",@"Jack",@"Kiss",@"张老师",@"李老师",@"周老师",@"倪老师",@"朱教授",@"刘教授",@"周教授",@"姚教授",@"叶老师",@"张教授",@"陶老师",@"陈教授", nil];
    
    SubtitleArray = [[NSMutableArray alloc]initWithObjects:@"教学科目：数学",@"教学科目：化学",@"教学科目：语文",@"教学科目：物理",@"教学科目：计算机",@"教学科目：挖掘机",@"教学科目：英语",@"教学科目：法语",@"教学科目：C语言",@"教学科目：钢琴",@"教学科目：小提琴",@"教学科目：滑板",@"教学科目：唱歌",@"教学科目：溜冰",@"教学科目：市场营销", nil];
    
}

-(void)viewWillAppear:(BOOL)animated {
    [_mapView viewWillAppear];
    _mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
    _locService.delegate = self;
    [self startLocation];
}

-(void)viewWillDisappear:(BOOL)animated {
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; // 不用时，置nil
    _locService.delegate = nil;
}
- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
- (void)dealloc {
    if (_mapView) {
        _mapView = nil;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)startLocation{
    
    NSLog(@"进入普通定位态");
    [_locService startUserLocationService];
    _mapView.showsUserLocation = NO;//先关闭显示的定位图层
    _mapView.userTrackingMode = BMKUserTrackingModeNone;//设置定位的状态
    //    _mapView.showsUserLocation = YES;//显示定位图层
    
    
}


/**
 *在地图View将要启动定位时，会调用此函数
 *@param mapView 地图View
 */
- (void)willStartLocatingUser
{
    NSLog(@"start locate");
}

/**
 *用户方向更新后，会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    [_mapView updateLocationData:userLocation];
    NSLog(@"heading is %@",userLocation.heading);
    //    _mapView.showsUserLocation = NO;
    _mapView.userTrackingMode = BMKUserTrackingModeFollow;
    _mapView.showsUserLocation = YES;
    
    [self performSelector:@selector(stop) withObject:nil afterDelay:2.0f];
}

-(void)stop{
    
    [_locService stopUserLocationService];
    [self addPointAnnotation];
    
}
/**
 *用户位置更新后，会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    //    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    [_mapView updateLocationData:userLocation];
}

/**
 *在地图View停止定位后，会调用此函数
 *@param mapView 地图View
 */
- (void)didStopLocatingUser
{
    NSLog(@"stop locate");
}

/**
 *定位失败后，会调用此函数
 *@param mapView 地图View
 *@param error 错误号，参考CLError.h中定义的错误号
 */
- (void)didFailToLocateUserWithError:(NSError *)error
{
    NSLog(@"location error");
}


/**
 *点中底图空白处会回调此接口
 *@param mapview 地图View
 *@param coordinate 空白处坐标点的经纬度
 */
//- (void)mapView:(BMKMapView *)mapView onClickedMapBlank:(CLLocationCoordinate2D)coordinate
//{
//    NSLog(@"onClickedMapBlank-latitude==%f,longitude==%f",coordinate.latitude,coordinate.longitude);
//    NSString* showmeg = [NSString stringWithFormat:@"您点击了地图空白处(blank click).\r\n当前经度:%f,当前纬度:%f,\r\nZoomLevel=%d;RotateAngle=%d;OverlookAngle=%d", coordinate.longitude,coordinate.latitude,
//                         (int)_mapView.zoomLevel,_mapView.rotation,_mapView.overlooking];
//    NSLog(@"%@",showmeg);
//}


//添加标注
- (void)addPointAnnotation
{
    if (pointAnnotation == nil) {
        for (int i = 0; i<latitudeArray.count; i++) {
            
            
            pointAnnotation = [[BMKPointAnnotation alloc]init];
            CLLocationCoordinate2D coor;
            coor.latitude = [latitudeArray[i] floatValue];
            coor.longitude = [longitudeArray[i] floatValue];
            pointAnnotation.coordinate = coor;
            pointAnnotation.title = TitleArray[i];
            pointAnnotation.subtitle = SubtitleArray[i];
            [_mapView addAnnotation:pointAnnotation];
            
            
        }
        //显示气泡
        [_mapView selectAnnotation:pointAnnotation animated:YES];
    }
}
// 根据anntation生成对应的View
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    //    //普通annotation
    //    if (annotation == pointAnnotation) {
    NSString *AnnotationViewID = @"renameMark";
    BMKPinAnnotationView *annotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
    if (annotationView == nil) {
        annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
        // 设置颜色
        annotationView.pinColor = BMKPinAnnotationColorPurple;
        // 从天上掉下效果
        annotationView.animatesDrop = YES;
        // 设置可拖拽
        //            annotationView.draggable = YES;
    }
    return annotationView;
    //    }
    //
    //    //动画annotation
    //    NSString *AnnotationViewID = @"AnimatedAnnotation";
    //    MyAnimatedAnnotationView *annotationView = nil;
    //    if (annotationView == nil) {
    //        annotationView = [[MyAnimatedAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
    //    }
    //    NSMutableArray *images = [NSMutableArray array];
    //    for (int i = 1; i < 4; i++) {
    //        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"poi_%d.png", i]];
    //        [images addObject:image];
    //    }
    //    annotationView.annotationImages = images;
    //    return annotationView;
    
}


@end
